var express = require('express');
var router = express.Router();
var {
  movie_model,
  user_model
} = require('../utils/model')
var {
  aesDecrypt
} = require("../utils/index")
// Router express 路由模块  根据不同的url 去匹配对应的路由 加载对应的资源 
// get get 请求 
// req 请求 前端发给后端的请求 url headers
// res 响应  后端返回给前端的数据响应
// res.send   返回数据 string json 
// res.render(path,data)  渲染一个ejs 页面  data 传入到页面的参数  
// res.json 
/* GET home page. */


router.get('/login', (req, res) => {
  var username = req.query.username ? aesDecrypt(req.query.username) : req.query.username
  res.render('login', {
    username
  })
})

router.get('/register', (req, res) => {
  res.render('register')
})

router.get('/mine', (req, res) => {
  res.render('mine')
})

router.get('/logout', (req, res) => {
  // 退出登录  
  req.session.destroy(() => {
    res.redirect('/')
  })
})

router.all('/list', (req, res) => {
  const query = req.query
  var obj = {}
  var sort = {}
  var keyword = query.keyword
  if (keyword) {
    obj = {
      $or :[
        {
          title: new RegExp(keyword)
        },
        {
          year: new RegExp(keyword)
        },
        {
          genres: new RegExp(keyword)
        }
      ]
    }
  }else{
    sort = query
  }
  // console.log(query);

  movie_model.find(obj, {
      _id: 0
    })
    .sort(sort)
    .then(result => {
      res.render('list', {
        result
      })
    }).catch(err => {
      res.send("<script>alert('服务器异常');location.href='/list'</script>")
    })
})

router.all('/mv',(req,res)=>{
  movie_model.findOne({
    id:req.query.mid
  })
  .then(result=>{
    res.render('movie',{
      result
    })
  }) .catch(err=>{
    res.send("<script>alert('服务器异常');location.href='/list'</script>")
  })
})


router.get('/', function (req, res, next) {
  res.render('index', {
    title: '千峰后台管理系统',
    username: req.session.username
  });
});

router.get('/demo', (req, res) => {
  res.send('<h2>wuhan good style</h2>')
})

router.get('/data', function (req, res) {
  res.json({
    code: 200,
    type: 1,
    msg: '获取数据成功'
  })
})
router.get('/some', function (req, res) {
  res.json({
    code: 400,
    msg: '获取数据成功',
    result: null
  })
})
router.get('/home', (req, res) => {
  res.render('home', {
    msg: 'hahahah',
    flag: !!0,
    tag: '<h1>hello word</h1>',
    username: ''
  })
})


router.get('/todos/:who/:time?', function (req, res) {
  res.json({
    url: req.url,
    headers: req.headers,
    query: req.query,
    params: req.params,
    msg: '获取req的参数'
  })
})

router.get('/movie', function (req, res) {
  movie_model.find({}, {})
    .limit(req.query.limit )
    .then(result => {
      res.json({
        code: 200,
        msg: '获取电影数据成功',
        result: result,
        query: req.query
      })
    })
})
//最基本的node接口
router.get('/userlist', (req, res) => {
  user_model.find({}, {})
    .then(result => {
      res.json({
        code: 200,
        msg: '请求成功',
        result: result
      })
    })
})

// post 请求
router.post('/submit', function (req, res) {
  res.json({
    code: 200,
    msg: '提交成功',
    body: req.body, //post 提交的参数
    result: null
  })
})

router.all('/all', function (req, res) {
  res.send('这是一个get/post请求')
})

module.exports = router;